﻿Imports System.IO.Ports
Module CommModele 'COM端口模块
    Public Function GetAvaliableSerialPorts() As String() '获取端口名字 返回字符串数组
        Dim portNames() As String = SerialPort.GetPortNames()
        Array.Sort(portNames)
        Return portNames
    End Function
End Module
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '窗口创建完毕后 就进行初始化
        Control.CheckForIllegalCrossThreadCalls = False


        Dim names() As String '定义字符串数组变量names 
        names = GetAvaliableSerialPorts() '获取端口
        cboPort.Items.AddRange(names) '向ComboBox加入
        cboSpeed.SelectedIndex = 0
    End Sub




























    Private Sub MSComm1_OnComm(sender As Object, e As EventArgs) Handles MSComm1.OnComm
        Select Case MSComm1.CommEvent
            Case 1'1就是comEvSend

            Case 2 '2就是comEvReceive
                TXTBox_Receive.Text += MSComm1.Input
        End Select
    End Sub

    Private Sub BTN_Send_Click(sender As Object, e As EventArgs) Handles BTN_Send.Click
        MSComm1.Output = TXTBox_Send.Text + vbCrLf
    End Sub

    Private Sub BTN_TurnOn_Click(sender As Object, e As EventArgs) Handles BTN_TurnOn.Click
        Dim port As Integer
        port = Val(cboPort.Text.Last())
        MSComm1.CommPort = port

        MSComm1.Settings = cboSpeed.Text
        MSComm1.PortOpen = True
        MSComm1.RThreshold = 2
        MSComm1.SThreshold = 2



        BTN_TurnOn.Enabled = False
        BTN_TurnDown.Enabled = True
    End Sub

    Private Sub BTN_TurnDown_Click(sender As Object, e As EventArgs) Handles BTN_TurnDown.Click
        MSComm1.PortOpen = False

        BTN_TurnDown.Enabled = False
        BTN_TurnOn.Enabled = True
    End Sub

    Private Sub BTN_Reload_Click(sender As Object, e As EventArgs) Handles BTN_Reload.Click
        cboPort.Items.Clear() '刷新串口前 先清除所有
        Dim names() As String '定义字符串数组变量names 
        names = GetAvaliableSerialPorts() '获取端口
        cboPort.Items.AddRange(names) '向ComboBox加入
    End Sub

    Private Sub BTN_Up_Click(sender As Object, e As EventArgs) Handles BTN_Up.Click
        MSComm1.Output = "S0 M03" + vbCrLf
    End Sub

    Private Sub BTN_Down_Click(sender As Object, e As EventArgs) Handles BTN_Down.Click
        MSComm1.Output = "S400 M03" + vbCrLf
    End Sub

    Private Sub BTN_Set_Click(sender As Object, e As EventArgs) Handles BTN_Set.Click
        MSComm1.Output = "G92 X0 Y0 Z0" + vbCrLf
    End Sub

    Private Sub BTN_Back_Click(sender As Object, e As EventArgs) Handles BTN_Back.Click
        MSComm1.Output = "G90 X0 Y0" + vbCrLf
    End Sub

    Private Sub BTN_yy_Click(sender As Object, e As EventArgs) Handles BTN_yy.Click
        MSComm1.Output = "G91 Y" + TXTBox_xy.Text + vbCrLf
    End Sub

    Private Sub BTN_y_Click(sender As Object, e As EventArgs) Handles BTN_y.Click
        MSComm1.Output = "G91 Y-" + TXTBox_xy.Text + vbCrLf
    End Sub

    Private Sub BTN_xx_Click(sender As Object, e As EventArgs) Handles BTN_xx.Click
        MSComm1.Output = "G91 X" + TXTBox_xy.Text + vbCrLf
    End Sub

    Private Sub BTN_x_Click(sender As Object, e As EventArgs) Handles BTN_x.Click
        MSComm1.Output = "G91 X-" + TXTBox_xy.Text + vbCrLf
    End Sub

    Private Sub TXTBox_Receive_TextChanged(sender As Object, e As EventArgs) Handles TXTBox_Receive.TextChanged
        TXTBox_Receive.Select(TXTBox_Receive.TextLength, 0)
        TXTBox_Receive.ScrollToCaret()
    End Sub
End Class
